//
//  MainViewController.m
//  govern
//
//  Created by LiangLigeng on 14-8-18.
//  Copyright (c) 2014年 LiangLigeng. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "CommendViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "OrderViewController.h"
#import "BaseNavigationController.h"
#import "SocketManager.h"
#import "MPNotificationView.h"
#import <AudioToolbox/AudioToolbox.h>

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.tabBar setHidden:YES];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
#ifdef __IPHONE_7_0
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
#endif
    
    [self _initViewController];
    
    [self _initTabbarView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newPush:) name:@"newpush" object:nil];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [SocketManager shareInstance];
}

-(void)newPush:(NSNotification *)notifacation
{
    NSLog(@"%@",@"xintuisong");
    NSString *title=(NSString *)notifacation.object;
    [MPNotificationView notifyWithText:@"热点新闻" andDetail:title];
    AudioServicesPlaySystemSound(1007);
}

//初始化子控制器
- (void)_initViewController {
    
    HomeViewController *home = [[HomeViewController alloc] init];
    CommendViewController *commend = [[CommendViewController alloc]init];
    ProfileViewController *profile = [[ProfileViewController alloc]init];
    DiscoverViewController *discover = [[DiscoverViewController alloc] init] ;
    OrderViewController *order = [[OrderViewController alloc] init] ;
    
    NSArray *views = @[home,order,discover,commend,profile];
    NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:5];
    for (UIViewController *viewController in views) {
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:viewController];
        [viewControllers addObject:nav];
    }
    
    self.viewControllers = viewControllers;
}

//创建自定义tabBar
- (void)_initTabbarView {
    _tabButtons=[[NSMutableArray alloc]init];
    _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-49, ScreenWidth, 49)];
    _tabbarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"p2_bottom_bar.png"]];
    [self.view addSubview:_tabbarView];
    
    //UIImageView *tabbarGroundImage = [UIFactory createImageView:@"tabbar_background.png"];
    //tabbarGroundImage.frame = _tabbarView.bounds;
    //[_tabbarView addSubview:tabbarGroundImage];
    
    NSArray *backgroud = @[@"btn_home_nov.png",@"btn_subscription_nov.png",@"btn_square_nov.png",@"btn_recommend_nov.png",@"btn_setup_nov.png"];
    
    NSArray *heightBackground = @[@"btn_home.png",@"btn_subscription.png",@"btn_square.png",@"btn_recommend.png",@"btn_setup.png"];
    
    for (int i=0; i<backgroud.count; i++) {
        NSString *backImage = backgroud[i];
        NSString *heightImage = heightBackground[i];
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((64-30)/2+(i*64), (49-30)/2, 30, 30);
        button.tag = i;
        [button setImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:heightImage] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            button.selected=YES;
        }
        [_tabbarView addSubview:button];
        [_tabButtons addObject:button];
    }
    
    //_sliderView = [[UIFactory createImageView:@"tabbar_slider.png"] retain];
    _sliderView.backgroundColor = [UIColor clearColor];
    _sliderView.frame = CGRectMake((64-15)/2, 5, 15, 44);
    [_tabbarView addSubview:_sliderView];
}

#pragma mark - actions
//tab 按钮的点击事件
- (void)selectedTab:(UIButton *)button {
    self.selectedIndex = button.tag;
    for (int i=0; i<5; i++) {
        UIButton *b=_tabButtons[i];
        b.selected=NO;
    }
    button.selected=YES;
    //float x = button.left + (button.width-_sliderView.width)/2;
    //[UIView animateWithDuration:0.2 animations:^{
    //    _sliderView.left = x;
    //}];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
